<?php
declare (strict_types = 1);
namespace app\admin\controller;

use think\Request;
use think\facade\{View, Session};
use think\exception\ValidateException;

use app\admin\validate\CashValidate;

use app\admin\model\{Users, Cash, Admin};

class CashController extends BaseController
{
    private $cashModel;
    private $token;
    public function __construct(Cash $cashModel)
    {
        $this->cashModel = $cashModel;
        $this->token = Session::get('token');
    }

    public function cash_list()
    {
        $adminModel = new Admin();

        if($this->token['role_id'] < 2){
            $where = [['role_id', '=', 2]];
        }else{
            $where = ['id' => $this->token['id']];
        }

        $admin_list = $adminModel->getList($where, 0);

        if($admin_list['code'] != 'ok'){
            $admin_list['data'] = [];
        }

        View::assign('admin_list', $admin_list['data']);
        View::assign('token', $this->token['role_id']);
        return view();
    }

    // 获取提现数据列表
    public function cash_data()
    {
        $userModel = new Users();
        $adminModel = new Admin();
        $data = input('get.');

        // 验证数据
        try{
            validate(CashValidate::class)->scene('list')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        if($this->token['role_id'] == 1){
            $admin_list = $adminModel->getList(['pid' => $this->token['id']], 'id');

            if($admin_list['code'] == 'ok'){
                $ids = array_column($admin_list['data']->toArray(), 'id');

                $where[] = ['admin_id', 'in', $ids];
            }

            $where[] = ['user_type', '<>', 3];
        }else if($this->token['role_id'] == 2){
            $where[] = ['admin_id', '=', $this->token['id']];
            $where[] = ['user_type', '<>', 3];
        }else{
            $where[] = ['user_type', '=', 3];
        }

        // 搜索条件
        if($data && isset($data['key'])){
            if(!empty($data['key']['start']) && empty($data['key']['end'])){
                $where[] = ['create_time', '>', strtotime($data['key']['start'])];
            }
    
            if(!empty($data['key']['end']) && empty($data['key']['start'])){
                $where[] = ['create_time', '<', strtotime($data['key']['end'])];
            }
    
            if(!empty($data['key']['start']) && !empty($data['key']['end'])){
                $where[] = ['create_time', 'between', [strtotime($data['key']['start']), strtotime($data['key']['end'])]];
            }

            if(!empty($data['key']['real_name'])){
                $where[] = ['real_name', 'like', '%' . $data['key']['real_name'] . '%'];
            }

            if(!empty($data['key']['status'])){
                $where[] = ['status', '=', $data['key']['status']];
            }

            if(!empty($data['key']['admin_id'])){
                $where[] = ['admin_id', '=', $data['key']['admin_id']];
            }
        }

        $count = $this->cashModel->getNum($where)['data'];

        $list = $this->cashModel->getList($where, 1, $data['page'], $data['limit'], '*', 'status, create_time DESC');

        if($list['code'] != 'ok'){
            $this->error_back('暂无数据~');
        }

        foreach($list['data'] as $k => $v){
            $admin_name = $adminModel->getVal(['id' => $v['admin_id']], 'nickname')['data'];

            if($v['user_type'] == 3){
                $v['nickname'] = $admin_name;
            }else{
                $v['nickname'] = $userModel->getVal(['id' => $v['user_id']], 'nickname')['data'];
            }

            $v['admin_name'] = $admin_name;
        }

        $this->success_back('ok', $list['data'], $count);
    }

    // 用户信息修改
    public function user_edit()
    {   
        $userModel = new Users();
        $data = input('post.');
        $editArr = ['id' => $data['id'], $data['field'] => $data['value']];

        // 验证数据
        try{
            validate(UsersValidate::class)->scene($data['field'] . 'Edit')->check($editArr);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }
        
        $flag = $userModel->edit($editArr);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('修改成功');
    }

    // 通过
    public function cash_pass()
    {   
        $cashModel = new Cash();
        $data = input('post.');

        // 验证数据
        try{
            validate(CashValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $data['status'] = 2;
        $data['finish_time'] = time();

        $flag = $cashModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }

    // 驳回
    public function cash_back()
    {   
        $cashModel = new Cash();
        $data = input('post.');

        // 验证数据
        try{
            validate(CashValidate::class)->scene('back')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $data['status']      = 3;
        $data['fail_msg']    = $data['fail_msg'];
        $data['finish_time'] = time();

        $flag = $cashModel->edit($data);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('成功');
    }

    // 删除用户
    public function user_del()
    {
        $userModel = new Users();
        $data = input('post.');

        // 验证数据
        try{
            validate(UsersValidate::class)->scene('show')->check($data);
        }catch(ValidateException $e){
            $this->error_back($e->getError());
        }

        $flag = $userModel->del(['id' => $data['id']]);

        if($flag['code'] != 'ok'){
            $this->error_back($flag['msg']);
        }

        $this->success_back('删除成功');
    }
}
